package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemTeamIntelligenceBinding;
import com.sgb.kjwl.databinding.ItemUserCenterTeamIntelligenceBinding;
import com.sgb.kjwl.databinding.PopwindowTeamIntelligenceUserBinding;
import com.sgb.kjwl.model.entity.enterprise.UserCenterTeamIntelligenceEntity;
import com.sgb.kjwl.utils.DisplayUtil;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class UserCenterTeamIntelligenceAdapter extends BaseRecycleAdapter<UserCenterTeamIntelligenceEntity> {

    Context context;

    public UserCenterTeamIntelligenceAdapter(Context context, List<UserCenterTeamIntelligenceEntity> datas) {
        super(context, R.layout.item_user_center_team_intelligence, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, UserCenterTeamIntelligenceEntity userCenterTeamIntelligenceEntity) {
        holder.setBinding(BR.viewModel, userCenterTeamIntelligenceEntity);

        ItemUserCenterTeamIntelligenceBinding itemTeamIntelligenceBinding = (ItemUserCenterTeamIntelligenceBinding) holder.getmBinding();
        itemTeamIntelligenceBinding.tvDate.setText(TextViewBindingAdapter.setTextForColon("资质类型:" + userCenterTeamIntelligenceEntity.getTypeName()));
        itemTeamIntelligenceBinding.tvPrice.setText(TextViewBindingAdapter.setTextForColon("资质等级:" + userCenterTeamIntelligenceEntity.getGradeName()));
        holder.getItem().findViewById(R.id.tv_show_detail).setOnClickListener(
                view -> showDialog(userCenterTeamIntelligenceEntity));
    }


    private void showDialog(UserCenterTeamIntelligenceEntity UserCenterTeamIntelligenceEntity) {
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        dm = context.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeigh = dm.heightPixels;

        LayoutInflater inflater = LayoutInflater.from(context);
        PopwindowTeamIntelligenceUserBinding layout = DataBindingUtil.inflate(inflater, R.layout.popwindow_team_intelligence_user, null, false);
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(layout.getRoot());
        builder.setCancelable(true);
        dialog = builder.show();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        CardView cv = dialog.findViewById(R.id.cv_dialog);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) cv.getLayoutParams(); //取控件textView当前的布局参数
        linearParams.width = DisplayUtil.dip2px(context, 270);//
        linearParams.height = screenHeigh / 3;//
        cv.setLayoutParams(linearParams);

        if (UserCenterTeamIntelligenceEntity != null) {
            layout.setViewModel(UserCenterTeamIntelligenceEntity);
            layout.tvTime.setText(TextViewBindingAdapter.setTextForColon("资质类型:" + UserCenterTeamIntelligenceEntity.getTypeName()));
            layout.tvDate.setText(TextViewBindingAdapter.setTextForColon("资质等级:" + UserCenterTeamIntelligenceEntity.getGradeName()));
        }
        dialog.findViewById(R.id.iv_cancel).setOnClickListener(v -> dialog.dismiss());
    }
}